{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"id": "ebcb7d83",
"metadata": {
"ExecuteTime": {
"end_time": "2023-12-19T10:09:08.122602Z",
"start_time": "2023-12-19T10:09:08.084460Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" V1 | \n",
" V2 | \n",
" V3 | \n",
" V4 | \n",
" V5 | \n",
" V6 | \n",
" V7 | \n",
" V8 | \n",
" 存货 | \n",
"
\n",
" \n",
" \n",
" \n",
" W1 | \n",
" 6 | \n",
" 2 | \n",
" 6 | \n",
" 7 | \n",
" 4 | \n",
" 2 | \n",
" 5 | \n",
" 9 | \n",
" 60 | \n",
"
\n",
" \n",
" W2 | \n",
" 4 | \n",
" 9 | \n",
" 9 | \n",
" 5 | \n",
" 3 | \n",
" 8 | \n",
" 5 | \n",
" 8 | \n",
" 55 | \n",
"
\n",
" \n",
" W3 | \n",
" 5 | \n",
" 2 | \n",
" 2 | \n",
" 1 | \n",
" 9 | \n",
" 4 | \n",
" 3 | \n",
" 3 | \n",
" 51 | \n",
"
\n",
" \n",
" W4 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 3 | \n",
" 9 | \n",
" 2 | \n",
" 7 | \n",
" 43 | \n",
"
\n",
" \n",
" W5 | \n",
" 2 | \n",
" 3 | \n",
" 9 | \n",
" 5 | \n",
" 7 | \n",
" 2 | \n",
" 6 | \n",
" 5 | \n",
" 41 | \n",
"
\n",
" \n",
" W6 | \n",
" 5 | \n",
" 5 | \n",
" 2 | \n",
" 2 | \n",
" 8 | \n",
" 1 | \n",
" 4 | \n",
" 3 | \n",
" 52 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" V1 V2 V3 V4 V5 V6 V7 V8 存货\n",
"W1 6 2 6 7 4 2 5 9 60\n",
"W2 4 9 9 5 3 8 5 8 55\n",
"W3 5 2 2 1 9 4 3 3 51\n",
"W4 7 6 6 7 3 9 2 7 43\n",
"W5 2 3 9 5 7 2 6 5 41\n",
"W6 5 5 2 2 8 1 4 3 52"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import permutations\n",
"import pandas as pd\n",
"import pulp as lp\n",
"\n",
"\n",
"# Defining the data\n",
"data = {\n",
" \"V1\": [6, 4, 5, 7, 2, 5],\n",
" 'V2': [2, 9, 2, 6, 3, 5],\n",
" \"V3\": [6, 9, 2, 6, 9, 2],\n",
" \"V4\": [7, 5, 1, 7, 5, 2],\n",
" \"V5\": [4, 3, 9, 3, 7, 8],\n",
" \"V6\": [2, 8, 4, 9, 2, 1],\n",
" \"V7\": [5, 5, 3, 2, 6, 4],\n",
" \"V8\": [9, 8, 3, 7, 5, 3],\n",
" \"存货\": [60, 55, 51, 43, 41, 52]\n",
"}\n",
"\n",
"# Creating the DataFrame\n",
"df = pd.DataFrame(data, index=[\"W1\", \"W2\", \"W3\", \"W4\", \"W5\", \"W6\"])\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "757bd77d",
"metadata": {
"ExecuteTime": {
"end_time": "2023-12-19T10:09:08.670319Z",
"start_time": "2023-12-19T10:09:08.538767Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" V1 V2 V3 V4 V5 V6 V7 V8 存货\n",
"W1 6 2 6 7 4 2 5 9 60.0\n",
"W2 4 9 9 5 3 8 5 8 55.0\n",
"W3 5 2 2 1 9 4 3 3 51.0\n",
"W4 7 6 6 7 3 9 2 7 43.0\n",
"W5 2 3 9 5 7 2 6 5 41.0\n",
"W6 5 5 2 2 8 1 4 3 52.0\n",
"需求量 35 37 22 32 41 32 43 38 NaN\n",
"596.0\n"
]
}
],
"source": [
"# Demand\n",
"demand = {\"V1\": 35,\"V2\":37, \"V3\": 22, \"V4\": 32, \"V5\": 41, \"V6\": 32, \"V7\": 43, \"V8\": 38}\n",
"\n",
"# Adding demand as a row\n",
"df = df._append(demand, ignore_index=True)\n",
"df.index = [\"W1\", \"W2\", \"W3\", \"W4\", \"W5\", \"W6\", \"需求量\"]\n",
"\n",
"print(df)\n",
"I=list(df.index)\n",
"I.pop()\n",
"J=list(df.columns)\n",
"J.pop()\n",
"\n",
"model = lp.LpProblem('distribution',sense=lp.LpMinimize) # 模型初始化\n",
"x = lp.LpVariable.dicts('x',indices=[(i,j) for i in I for j in J],lowBound=0,cat='Integer')\n",
"obj = lp.lpSum(df.loc[i,j]*x[i,j] for i in I for j in J)\n",
"model += obj # 先添加目标函数\n",
"for j in J:\n",
" model += lp.lpSum(x[i,j] for i in I)==df.loc['需求量',j]\n",
"for i in I:\n",
" model += lp.lpSum(x[i,j] for j in J)<=df.loc[i,'存货'] \n",
"model.solve()\n",
"print(lp.value(model.objective))#result"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d146227c",
"metadata": {
"ExecuteTime": {
"end_time": "2023-12-19T10:09:11.629604Z",
"start_time": "2023-12-19T10:09:11.604328Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" V1 | \n",
" V2 | \n",
" V3 | \n",
" V4 | \n",
" V5 | \n",
" V6 | \n",
" V7 | \n",
" V8 | \n",
" 存货 | \n",
"
\n",
" \n",
" \n",
" \n",
" W1 | \n",
" 6 | \n",
" 2 | \n",
" 6 | \n",
" 7 | \n",
" 4 | \n",
" 2 | \n",
" 5 | \n",
" 9 | \n",
" 60.0 | \n",
"
\n",
" \n",
" W2 | \n",
" 4 | \n",
" 9 | \n",
" 9 | \n",
" 5 | \n",
" 3 | \n",
" 8 | \n",
" 5 | \n",
" 8 | \n",
" 55.0 | \n",
"
\n",
" \n",
" W3 | \n",
" 5 | \n",
" 2 | \n",
" 2 | \n",
" 1 | \n",
" 9 | \n",
" 4 | \n",
" 3 | \n",
" 3 | \n",
" 51.0 | \n",
"
\n",
" \n",
" W4 | \n",
" 7 | \n",
" 6 | \n",
" 6 | \n",
" 7 | \n",
" 3 | \n",
" 9 | \n",
" 2 | \n",
" 7 | \n",
" 43.0 | \n",
"
\n",
" \n",
" W5 | \n",
" 2 | \n",
" 3 | \n",
" 9 | \n",
" 5 | \n",
" 7 | \n",
" 2 | \n",
" 6 | \n",
" 5 | \n",
" 41.0 | \n",
"
\n",
" \n",
" W6 | \n",
" 5 | \n",
" 5 | \n",
" 2 | \n",
" 2 | \n",
" 8 | \n",
" 1 | \n",
" 4 | \n",
" 3 | \n",
" 52.0 | \n",
"
\n",
" \n",
" 需求量 | \n",
" 35 | \n",
" 37 | \n",
" 22 | \n",
" 32 | \n",
" 41 | \n",
" 32 | \n",
" 43 | \n",
" 38 | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" V1 V2 V3 V4 V5 V6 V7 V8 存货\n",
"W1 6 2 6 7 4 2 5 9 60.0\n",
"W2 4 9 9 5 3 8 5 8 55.0\n",
"W3 5 2 2 1 9 4 3 3 51.0\n",
"W4 7 6 6 7 3 9 2 7 43.0\n",
"W5 2 3 9 5 7 2 6 5 41.0\n",
"W6 5 5 2 2 8 1 4 3 52.0\n",
"需求量 35 37 22 32 41 32 43 38 NaN"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "006af2d5",
"metadata": {
"ExecuteTime": {
"end_time": "2023-12-19T10:09:12.937630Z",
"start_time": "2023-12-19T10:09:12.897242Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" V1 | \n",
" V2 | \n",
" V3 | \n",
" V4 | \n",
" V5 | \n",
" V6 | \n",
" V7 | \n",
" V8 | \n",
" 存货 | \n",
"
\n",
" \n",
" \n",
" \n",
" W1 | \n",
" 0 | \n",
" 37 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 21 | \n",
" 0 | \n",
" 0 | \n",
" 60.0 | \n",
"
\n",
" \n",
" W2 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 41 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 55.0 | \n",
"
\n",
" \n",
" W3 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 32 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 19 | \n",
" 51.0 | \n",
"
\n",
" \n",
" W4 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 43 | \n",
" 0 | \n",
" 43.0 | \n",
"
\n",
" \n",
" W5 | \n",
" 35 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 41.0 | \n",
"
\n",
" \n",
" W6 | \n",
" 0 | \n",
" 0 | \n",
" 22 | \n",
" 0 | \n",
" 0 | \n",
" 11 | \n",
" 0 | \n",
" 19 | \n",
" 52.0 | \n",
"
\n",
" \n",
" 需求量 | \n",
" 35 | \n",
" 37 | \n",
" 22 | \n",
" 32 | \n",
" 41 | \n",
" 32 | \n",
" 43 | \n",
" 38 | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" V1 V2 V3 V4 V5 V6 V7 V8 存货\n",
"W1 0 37 0 0 0 21 0 0 60.0\n",
"W2 0 0 0 0 41 0 0 0 55.0\n",
"W3 0 0 0 32 0 0 0 19 51.0\n",
"W4 0 0 0 0 0 0 43 0 43.0\n",
"W5 35 0 0 0 0 0 0 0 41.0\n",
"W6 0 0 22 0 0 11 0 19 52.0\n",
"需求量 35 37 22 32 41 32 43 38 NaN"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1 = df.copy()\n",
"for i in I:\n",
" for j in J:\n",
" df1.loc[i,j] = lp.value(x[i,j])\n",
"df1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3d62a0b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}